﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CipherBox
{
    class RemotePath
    {
        string m_RelativeDirectory;
        string m_File;
        string m_RootDirectory;
        public string RootDirectory { get { return Dir(m_RootDirectory); } }
        public string RelativeDirectory { get { return Dir(m_RelativeDirectory); } }
        public string RelativeFile { get { return Dir(m_RelativeDirectory) +  m_File; } }
        public string File { get { return m_File; } }
        public string FullPath { get { return Dir(m_RootDirectory, m_RelativeDirectory) + m_File; } }
        public string FullDirectory { get { return Dir(m_RootDirectory, m_RelativeDirectory); } }
        private string Dir(params string[]  strings)
        {
           return replaceDoubleSlashes("/" + string.Join("/", strings) + "/");
        }
        private string replaceDoubleSlashes(string s)
        {
            string t = s.Replace("//", "/");
            if (t != s)
            {
                return replaceDoubleSlashes(t);
            }
            return t;
        }
        public RemotePath(string path, string rootPath)
        {
            m_RootDirectory = rootPath;
            path = path.Replace(rootPath, string.Empty);

            string[] remoteDir = path.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
            List<string> t = remoteDir.ToList<string>();
            if (!path.EndsWith("/"))
            {
                m_File = t[t.Count - 1];
                t.RemoveAt(remoteDir.Length - 1);
            }
            else
            {
                m_File = string.Empty;
            }
            remoteDir = t.ToArray<string>();
            m_RelativeDirectory = "/" + string.Join("/", remoteDir);
        }
    }
}
